package com.sigmazero13.MKDummy.utils;

import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;

import com.sigmazero13.MKDummy.models.ManaColor;

public class StringUtils {
	/**
	 * Given either a Spannable String or a regular String and a token, apply
	 * the given CharacterStyle to the span between the tokens, and also
	 * remove tokens.
	 * <p>
	 * For example, {@code setSpanBetweenTokens("Hello ##world##!", "##",
	 * new ForegroundColorSpan(0xFFFF0000));} will return a CharSequence
	 * {@code "Hello world!"} with {@code world} in red.
	 *
	 * @param text The text, with the tokens, to adjust.
	 * @param token The token string; there should be at least two instances
	 *             of token in text.
	 * @param cs The style to apply to the CharSequence. WARNING: You cannot
	 *            send the same two instances of this parameter, otherwise
	 *            the second call will remove the original span.
	 * @return A Spannable CharSequence with the new style applied.
	 *
	 * @see http://developer.android.com/reference/android/text/style/CharacterStyle.html
	 */
	public static CharSequence setSpanBetweenTokens(CharSequence text, String token, CharacterStyle... cs)
	{
	    // Start and end refer to the points where the span will apply
		while (text.toString().contains(token)) {
		    int tokenLen = token.length();
		    int start = text.toString().indexOf(token) + tokenLen;
		    int end = text.toString().indexOf(token, start);
		    
		    if (start > -1 && end > -1)
		    {
		        // Copy the spannable string to a mutable spannable string
		        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
		        for (CharacterStyle c : cs)
		            ssb.setSpan(CharacterStyle.wrap(c), start, end, 0);
	
		        // Delete the tokens before and after the span
		        ssb.delete(end, end + tokenLen);
		        ssb.delete(start - tokenLen, start);
	
		        text = ssb;
		    } else {
		    	break;
		    }
		}

	    return text;
	}
	
	public static CharSequence formatMessage(CharSequence message) {
		message = setSpanBetweenTokens(message, "#b#", new StyleSpan(Typeface.BOLD));
		message = setSpanBetweenTokens(message, "#i#", new StyleSpan(Typeface.ITALIC));
		for (ManaColor c : ManaColor.values()) {
			message = setSpanBetweenTokens(message, "#"+c.getCode()+"#", new ForegroundColorSpan(c.getColor()));
		}
		return message;
	}
}
